package com.totoro.service;

import java.util.Collection;
import java.util.Iterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.totoro.dao.BaseHibernateDao;
import com.totoro.dao.ReviewProjectDao;
import com.totoro.domain.Project;
import com.totoro.domain.ReviewProject;
import com.totoro.domain.Tutorial;
import com.totoro.domain.key.ReviewProjectKey;

/*
 * 评阅论文service
 */
@Transactional
@Service
public class ReviewProjectService extends BaseService<ReviewProject, ReviewProjectKey>{

	private ReviewProjectDao reviewProjectDao;
	
	@Autowired
	@Override
	public void setBaseDao(BaseHibernateDao<ReviewProject, ReviewProjectKey> reviewProjectDao) {
		// TODO Auto-generated method stub
		this.baseDao = reviewProjectDao;
		this.reviewProjectDao = (ReviewProjectDao) reviewProjectDao;
	}
	
	public ReviewProject getAverageReviewScoreByProject(Project project) {
		Collection<ReviewProject> ReviewProjects = project.getReviewProject();
		int docQualityScore = 0;
		int achievementLevel = 0;
		//是否所有评阅老师已填写评阅成绩
		boolean state = true;
		
		//循环取得平均成绩
		for (Iterator iterator = ReviewProjects.iterator(); iterator.hasNext();) {
			ReviewProject reviewProject = (ReviewProject) iterator.next();
			if(reviewProject.getAchievementLevel() != null) {
				achievementLevel += reviewProject.getAchievementLevel();
			}
			if (reviewProject.getDocQualityScore() != null) {
				docQualityScore += reviewProject.getDocQualityScore();
			}
			if(reviewProject.getState() != null) {
				state = state && reviewProject.getState();
			} else {
				state = false;
			}
		}
		int size = ReviewProjects.size();
		
		ReviewProject averageReviewProject = new ReviewProject();
		averageReviewProject.setAchievementLevel(achievementLevel/size);
		averageReviewProject.setDocQualityScore(docQualityScore/size);
		
		averageReviewProject.setState(state);
		
		return averageReviewProject;
	}
}
